package com.mlnx.bluwifi.tp.zhengkbp;

/**
 * Created by amanda.shan on 2017/9/13.
 */

public class BpResponse {

    public static byte[] OK = new byte[]{0x00, 0x00, 0x2F};
    public static byte[] ERROR1 = new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0x9B};
    public static byte[] ERROR2 = new byte[]{(byte) 0x00, (byte) 0xFF, (byte) 0x1A};

    /**
     * 和固定返回指令匹配
     *
     * @param compare
     * @return 0 匹配成功指令  1 匹配错误指令  -1 未匹配到指令
     */
    public static int compareByte(byte[] compare) {

//        final StringBuilder stringBuilder = new StringBuilder(compare.length);
//        for (byte byteChar : compare)
//            stringBuilder.append(String.format("%02X ", byteChar));
//        LogUtils.d("内容：" + stringBuilder.toString());

        if (compare.length == OK.length) {
            boolean ok = true;
            for (int i = 0; i < compare.length; i++) {
                if (compare[i] != OK[i]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                return 0;
            }
        }

        if (compare.length == ERROR1.length) {
            boolean ok = true;
            for (int i = 0; i < compare.length; i++) {
                if (compare[i] != ERROR1[i]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                return 1;
            }
        }

        if (compare.length == ERROR2.length) {
            boolean ok = true;
            for (int i = 0; i < compare.length; i++) {
                if (compare[i] != ERROR2[i]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                return 2;
            }
        }

        return -1;
    }

    /**
     * ERR1：传感器震荡异常
     * ERR2：检测不到足够的心跳或算不出血压
     * ERR3：测量结果异常
     * ERR4：袖带过松或漏气(10秒内加压不到30mmHg)
     * ERR5：气管被堵住
     * ERR6：测量时压力波动大
     * ERR7：压力超过上限
     *
     * @param code
     * @return
     */
    public static String getErrorMsg(int code) {
        if (code == 1)
            return "传感器震荡异常";
        else if (code == 2)
            return "检测不到足够的心跳或算不出血压";
        else if (code == 3)
            return "测量结果异常";
        else if (code == 4)
            return "袖带过松或漏气";
        else if (code == 5)
            return "气管被堵住";
        else if (code == 6)
            return "测量时压力波动大";
        else if (code == 7)
            return "压力超过上限";
        else return "测量错误";
    }

}
